package com.pangda.service.impl;

import com.pangda.pojo.Dept;
import com.pangda.mapper.DeptMapper;
import com.pangda.pojo.vo.NodeVo;
import com.pangda.service.IDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-20
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Autowired
    private DeptMapper mapper;
    @Override
    public List<Dept> listExclude(Integer deptId) {
        List<Dept> list = mapper.selectList(null);
        List<Dept> allChildrens = getAllChildrens(deptId, list);
        List<Dept> collect = list.stream().filter(dept -> allChildrens.stream().anyMatch(child -> !(dept.getDeptId().equals(child.getDeptId()) || dept.getDeptId().equals(deptId)))).collect(Collectors.toList());
        return collect;
    }

    /**\
     * 查询部门层级树
     * @param
     * @return
     */
    @Override
    public List<NodeVo> getTree() throws IOException {
        List<Dept> depts = mapper.selectList(null);
        List<NodeVo> menuList = getMenuList(depts);
        return menuList;
    }

    @Override
    public NodeVo getTreeById(Integer roleId) {

        List<Dept> depts = mapper.selectList(null);
             return null;
    }

    // 输入参数为父节点id和节点列表，返回值为该父节点下的所有子节点
    public List<Dept> getAllChildrens(Integer parentId, List<Dept> excludeList) {
        List<Dept> childIds = new ArrayList<>();
        getAllChildIds(parentId, excludeList, childIds);
        return childIds;
    }

    // 递归查询子节点的方法返回部门
    private void getAllChildIds(Integer parentId, List<Dept> depts, List<Dept> childIds) {
        for (Dept dept : depts) {
            if (dept.getParentId().equals(parentId)) {
                childIds.add(dept);
                getAllChildIds(dept.getDeptId(), depts, childIds); // 递归查询子节点的子节点
            }
        }
    }
    //获得树级结构菜单
    public List<NodeVo> getMenuList(List<Dept> list) throws IOException {
        //拿到菜单的所有数据
        //存储根节点的菜单，即一级菜单
        List<NodeVo> roots = new ArrayList<>();

        //遍历所有数据，找到根节点菜单
        for (Dept dept: list) {
            if(dept.getParentId().equals(0)){
                NodeVo root = new NodeVo();
                root.setId(dept.getDeptId());
                root.setLabel(dept.getDeptName());
                root.setChildren(new ArrayList<>());
                roots.add(root);
                //找到根节点菜单的时候，寻找这个根节点菜单下的子节点菜单。
                findChilds(root,list);
            }
        }
        return roots;
    }

    private void findChilds(NodeVo parentNode, List<Dept> list){
        List<NodeVo> childlist=new ArrayList<>();
        //遍历所有数据，找到是入参父节点的子节点的数据，然后加到childlist集合中。
        for (Dept dept : list) {
            if ((dept.getParentId().equals(parentNode.getId()))){
                NodeVo deptNode = new NodeVo();
                deptNode.setId(dept.getDeptId());
                deptNode.setLabel(dept.getDeptName());
                deptNode.setChildren(new ArrayList<>());
                childlist.add(deptNode);
            }

        }
        //若子节点不存在，那么就不必再遍历子节点中的子节点了 直接返回。
        if(childlist.size()==0){
            return;}
        //设置父节点的子节点列表
        parentNode.setChildren(childlist);
        //若子节点存在，接着递归调用该方法，寻找子节点的子节点。
        for (NodeVo childs :
                childlist) {
            findChilds(childs, list);
        }
    }





}
